﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web.Services.Protocols;

namespace Amf4Asmx.Web
{
    public class WebMethodData : WebObjectData
    {
        public MethodInfo Method { get; private set; }
        public string[] InSoapHeaders { get; private set; }
        public string[] OutSoapHeaders { get; private set; }

        public ParameterInfo[] Parameters { get; private set; }
        public WebMethodData(MethodInfo methodInfo)
        {
            this.Method = methodInfo;
            this.Parameters = methodInfo.GetParameters();
            InSoapHeaders = GetSoapHeaderNames(methodInfo.GetCustomAttributes(typeof(SoapHeaderAttribute), true), SoapHeaderDirection.In);
            OutSoapHeaders = GetSoapHeaderNames(methodInfo.GetCustomAttributes(typeof(SoapHeaderAttribute), true), SoapHeaderDirection.Out);
        }

        private string[] GetSoapHeaderNames(object[] attrs, SoapHeaderDirection soapHeaderDirection)
        {
            List<string> headerNames = new List<string>();
            foreach (object attr in attrs)
            {
                if (((attr as SoapHeaderAttribute).Direction & soapHeaderDirection) == soapHeaderDirection)
                {
                    headerNames.Add((attr as SoapHeaderAttribute).MemberName);
                }
            }
            return headerNames.ToArray();
        }


        public override object Instantiate(object properties)
        {
            object[] result = new object[Parameters.Length];
            if (properties is object[])
            {
                object[] propArray = (object[])properties;
                int propIndex = 0;
                for (int i = 0; i < Parameters.Length; i++)
                {
                    if (Parameters[i].IsIn || Parameters[i].Attributes == ParameterAttributes.None)
                    {
                        if (propArray.Length > propIndex)
                        {
                            result[i] = TypeHelper.Instantiate(Parameters[i].ParameterType, propArray[propIndex]);
                        }
                        else
                        {
                            result[i] = null; // TODO - default using attribute
                        }
                        propIndex++;
                    }
                    else
                    {
                        result[i] = null;
                    }
                }
            }
            return result;
        }

        //public Dictionary<string, object> GetOutputParameters(object[] args)
        //{
        //    Dictionary<string, object> result = new Dictionary<string, object>();
        //    for (int i = 0; i < parameters.Length; i++)
        //    {
        //        if (parameters[i].IsOut)
        //        {
        //            result[parameters[i].Name] = args[i];
        //        }
        //    }
        //    return result;
        //}

        public object BuildResult(object returnVal, object[] args)
        {
            List<object> result = new List<object>();
            if (returnVal != null)
            {
                result.Add(returnVal);
            }
            for (int i = 0; i < Parameters.Length; i++)
            {
                if (Parameters[i].IsOut)
                {
                    result.Add(args[i]);
                }
            }
            if (result.Count == 1)
            {
                return result[0];
            }
            return result.ToArray();
        }


        public bool ContainsSoapHeader(string p)
        {
            foreach (string s in InSoapHeaders)
            {
                if (s == p)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
